package oa.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;

import oa.entity.Attendance;
import oa.entity.Department;
import oa.entity.User;
import oa.paging.Page;
import oa.service.impl.AttendanceServiceImpl;
import oa.service.impl.DepartmentServiceImpl;
import oa.service.impl.UserServiceImpl;

/**
 * Servlet implementation class DeptStatistics
 */
@WebServlet("/deptstatistics")
public class DeptStatistics extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DeptStatistics() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session = request.getSession();		
		PrintWriter out = response.getWriter();
		UserServiceImpl user = new UserServiceImpl();
		DepartmentServiceImpl depart = new DepartmentServiceImpl();
		AttendanceServiceImpl attendan = new AttendanceServiceImpl();
		Attendance att = new Attendance();
		User us = new User();
		Map<String, Object> haplist = new HashMap<String, Object>(2);
		List<User>  usershowlist= new ArrayList<User>();
		Page<User> pageInfo = new Page<User>();		
		String opt = request.getParameter("opt");
		String pageno = request.getParameter("PageNo");
		if (pageno == null) {
			pageno = "1";
		}
		int companyidmoren = 0;
		
		String id = request.getParameter("companyidmoren");
		if(id==null) {
			id ="0";
		}
		if(id==null || id=="0") {
		  int companyid =(int)session.getAttribute("companyid");
			
			companyidmoren=companyid;
		}else {
			companyidmoren=Integer.parseInt(id);
		}
			
		
      
      if ("showdepart".equals(opt)) {
			 List<Department> departlist = new ArrayList<Department>();
			 if(companyidmoren==0) {
				 departlist=depart.selectDepartment(0, null, 0);
			 }else {
				 departlist=depart.selectDepartment(0, null, companyidmoren);
			 }
		     out.print(JSON.toJSONString(departlist));
        }else if("showdepartlist".equals(opt)) {
        	out.print(JSON.toJSONString(depart.selectDepartment(0, null, companyidmoren)));

        } else if("".equals(opt) || opt==null) {
        	session.setAttribute("DepartList", depart.selectDepartment(0, null, 0));
			request.getRequestDispatcher("dept_statistics.jsp").forward(request, response);
		
        } else {
        	
		
			
		}
		out.flush();
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
